package com.example.schoolenterprisealliance.activity;
//联盟介绍
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.schoolenterprisealliance.MainActivity;
import com.example.schoolenterprisealliance.R;
import com.example.schoolenterprisealliance.adapter.IntroduceAdapter;
import com.example.schoolenterprisealliance.adapter.NavigationAdapter;
import com.example.schoolenterprisealliance.bean.Admin;
import com.example.schoolenterprisealliance.bean.Navigation;
import com.example.schoolenterprisealliance.dialog.CertificateDialog;
import com.example.schoolenterprisealliance.dialog.MyDialog;
import com.example.schoolenterprisealliance.io.AppConfig;
import com.example.schoolenterprisealliance.method.Skip;
import com.example.schoolenterprisealliance.service.AdminService;

import java.util.ArrayList;
import java.util.List;

public class IntroduceActivity extends AppCompatActivity {
    private IntroduceAdapter introduceAdapter;
    private NavigationAdapter navigationAdapter;
    private RecyclerView alliance_introduction;
    private RecyclerView rv_navigation;
    private List<Admin.DataBean> data;
    private EditText et_search;
    private ImageView iv_search;
    private Intent intent;
    private Skip skip;//跳转

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introduce);
        //初始化控件

        alliance_introduction = findViewById(R.id.rv_introduce);
        et_search = findViewById(R.id.et_search);
        iv_search = findViewById(R.id.iv_search);
        rv_navigation = findViewById(R.id.rv_navigation);


        //一个是前一个activity的退出，另一个activity的进入。
        overridePendingTransition(R.animator.into_right, R.animator.out_left);




        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        //设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        alliance_introduction.setLayoutManager(linearLayoutManager);
        alliance_introduction.setOverScrollMode(View.OVER_SCROLL_NEVER);
        LinearLayoutManager linearLayoutManager1 = new GridLayoutManager(this, 5);
        linearLayoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        rv_navigation.setLayoutManager(linearLayoutManager1);
        navigationAdapter=new NavigationAdapter(R.layout.item_navigation, addNavigation());
        rv_navigation.setAdapter(navigationAdapter);
        adminData();
        navigationListener();
        ivImageIntent();
        searchIntent();
    }
    //添加联盟成员信息
    private void adminData() {
        //1, 声明Retrofit 的构建者
        Retrofit.Builder builder = new Retrofit.Builder();
        //2, 设置公共的地址
        builder.baseUrl(AppConfig.BASE_URL);
        //3, 设置解析gson
        builder.addConverterFactory(GsonConverterFactory.create());
        //4, 得到Retrofit 对象
        Retrofit retrofit = builder.build();
        //5, 得到接口
        AdminService adminService = retrofit.create(AdminService.class);
        //6, 得到Call 的方法
        Call<Admin> call = adminService.getAdminData();
        //7, 传入接口回调 , 获取数据
        call.enqueue(new Callback<Admin>() {
            @Override
            public void onResponse(Call<Admin> call, Response<Admin> response) {
//                获取到的数据
                Admin admin = response.body();
                //数据源
                data = admin.getData();
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getContext().equals("")) {
                        data.get(i).setContext("无描述");
                    }
                    data.get(i).setItemtype(i % 2);
                    Log.e("Admin_response", data.get(i).getContext());
                }
                introduceAdapter = new IntroduceAdapter(data);
                alliance_introduction.setAdapter(introduceAdapter);
                introduceAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                    @Override
                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                        switch (view.getId()) {
                            case R.id.button_left:
                                Log.d("button", "button_left被点击了");
                                String string1 = data.get(position).getCertificate();
                                CertificateDialog certificateDialog1 = new CertificateDialog(IntroduceActivity.this, R.layout.content_certificate_dialog, string1);
                                Window window1 = certificateDialog1.getWindow();
                                certificateDialog1.show();
                                certificateDialog1.setCancelable(false);
                                break;
                            case R.id.button_right:
                                Log.d("button", "button_right被点击了");
                                String string2 = data.get(position).getCertificate();
                                CertificateDialog certificateDialog2 = new CertificateDialog(IntroduceActivity.this, R.layout.content_certificate_dialog, string2);
                                Window window2 = certificateDialog2.getWindow();
                                certificateDialog2.show();
                                certificateDialog2.setCancelable(false);
                                break;
                        }
                    }
                });
            }

            @Override
            public void onFailure(Call<Admin> call, Throwable t) {
                Log.e("Adminerror", t.getMessage());
                t.printStackTrace();
            }
        });

    }
    //添加导航栏数据
    private List<Navigation> addNavigation() {
        List<Navigation> data = new ArrayList<>();
        Navigation navigation;
            for (int i=0;i<5;i++){
                navigation=new Navigation();
                if (i==0){
                    navigation.setId(i);
                    navigation.setNavigetion("首页");
                    data.add(navigation);
                    continue;
                }else if (i==1){
                    navigation.setId(i);
                    navigation.setNavigetion("联盟介绍");
                    data.add(navigation);
                    continue;
                }else if (i==2){
                    navigation.setId(i);
                    navigation.setNavigetion("最新动态");
                    data.add(navigation);
                    continue;
                }else if (i==3){
                    navigation.setId(i);
                    navigation.setNavigetion("最新政策");
                    data.add(navigation);
                    continue;
                }else {
                    navigation.setId(i);
                    navigation.setNavigetion("互联网+");
                    data.add(navigation);
                    continue;
                }
            }
            return data;
        }
    //导航栏监听
    private void navigationListener(){
        navigationAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TextView tv=view.findViewById(R.id.tv_navigation);
                String s=tv.getText().toString();
                if (s.equals("联盟介绍")){
                    TextView tv_border=view.findViewById(R.id.tv_border);
                    tv_border.setVisibility(View.VISIBLE);
                }else {
                    skip=new Skip(IntroduceActivity.this,s);
                    intent=skip.intent();
                    startActivity(intent);
                }

                Log.e("navagationListener",tv.getText().toString());
            }
        });
    }
   //点击搜索框跳转
    private void searchIntent(){
        et_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(IntroduceActivity.this,SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }
   // 点击搜索图片跳转
    private void ivImageIntent(){
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(IntroduceActivity.this,SearchHotActivity.class);
                startActivity(intent);
            }
        });
    }
}